import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "login",
    component: () => import("../views/Login/index.vue"),
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/home/index.vue"),
    children: [
      {
        path: "/LimitList",
        name: "LimitList",
        component: () => import("../views/home/limit/LimitList/index.vue"),
      },
      {
        path: "/RoleList",
        name: "RoleList",
        component: () => import("../views/home/limit/RoleList/index.vue"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

//全局路由守卫
router.beforeEach((to, form, next) => {
  if (to.path === "/") return next();
  const token = localStorage.getItem("token");
  if (!token) return next("/");
  next();
});

export default router;
